% autowrite prob
clc
clear all
load 'D:\My Documents\CIRANO\db_mySQL\tp_2011May29_MR7_WR_case2.mat' cohTpGrpStd durTpGrpStd durTtlStdVec durTtlStdMat NUMBER_OF_YEAR numGrp numEdgesStd avgCohTpGrpStd avgDurTpGrpStd;
load 'D:\My Documents\CIRANO\db_mySQL\CohLamdaAdjStd.mat' lamdaAdj ;
filename1='tp_2011May29_MR7_WR_case2.xls';
filename2='lamda_2012Mar5_MR7_WR.xls';
year_list=(1998:2009)';
year_list_text=num2str(year_list);

% autowrite matrices
for yearIdx=1:NUMBER_OF_YEAR
    sheetlist=strcat('',year_list_text(yearIdx,:),'');
    for cdIdx = 1: numGrp
        
        c(:,:)=cohTpGrpStd(cdIdx,yearIdx,:,:)./100;
        d(:,:)=durTpGrpStd(cdIdx,yearIdx,:,:)./100;
        
        p = 3+ 9*(cdIdx-1);
        xlswrite(filename1, c, sheetlist,strcat('b','',num2str(p),''));
        xlswrite(filename1, d, sheetlist,strcat('k','',num2str(p),''));
        clear c d
        
        grp = strcat('',num2str(cdIdx),'');
        xlswrite(filename1, {strcat('Group', '',grp,'')}, sheetlist,strcat('A','',num2str(p-1),''));
        
    end
end

% autowrite avg matrices
for cdIdx = 1: numGrp
        
        c(:,:)=avgCohTpGrpStd(cdIdx,:,:)./100;
        d(:,:)=avgDurTpGrpStd(cdIdx,:,:)./100;
        
        p = 3+ 9*(cdIdx-1);
        xlswrite('D:\My Documents\CIRANO\db_mySQL\tp_2012Mar5_MR7_WR.xls', c, 'Avg',strcat('b','',num2str(p),''));
        xlswrite('D:\My Documents\CIRANO\db_mySQL\tp_2012Mar5_MR7_WR.xls', d, 'Avg',strcat('k','',num2str(p),''));
        clear c d
        
        grp = strcat('',num2str(cdIdx),'');
        xlswrite('D:\My Documents\CIRANO\db_mySQL\tp_2012Mar5_MR7_WR.xls', {strcat('Group', '',grp,'')},'Avg',strcat('A','',num2str(p-1),''));
        
 end


% autowrite generator
transMatGrp = zeros(numGrp,NUMBER_OF_YEAR,numEdgesStd,numEdgesStd );
for yearIdx=1:NUMBER_OF_YEAR
    sheetlist=strcat('',year_list_text(yearIdx,:),'');
    for cdIdx = 1: numGrp
        
        c(:,:)=lamdaAdj(cdIdx, yearIdx,:,:);
        
        diagInd = logical(eye(numEdgesStd,numEdgesStd));
        totalsVec(:,:) = durTtlStdVec (cdIdx,yearIdx,1,:);
        totalsMat(:,:) = durTtlStdMat (cdIdx,yearIdx,:,:);
        totalsMat(diagInd) = 0;
        totalsMat(diagInd) = -sum(totalsMat,2);
        nonEmpty = find(totalsVec>0);
        d(nonEmpty,:) = bsxfun(@ldivide,...
            totalsVec(nonEmpty),totalsMat(nonEmpty,:));
        transMatGrp(cdIdx,yearIdx,:,:) = d;
        
        
        p = 3+ 9*(cdIdx-1);
        xlswrite(filename2, c, sheetlist,strcat('b','',num2str(p),''));
        xlswrite(filename2, d, sheetlist,strcat('k','',num2str(p),''));
        clear c d
        
        grp = strcat('',num2str(cdIdx),'');
        xlswrite(filename2, {strcat('Group', '',grp,'')}, sheetlist,strcat('A','',num2str(p-1),''));
        
    end
end

% avg generator
transMatGrp = zeros(numGrp,numEdgesStd,numEdgesStd );
for cdIdx = 1%: numGrp
    diagInd = logical(eye(numEdgesStd,numEdgesStd));
    totalsVec(:,:) = sum(durTtlStdVec(cdIdx,:,:,:),2);
    totalsMat(:,:) = sum(durTtlStdMat(cdIdx,:,:,:),2);
    
    totalsMat(diagInd) = 0;
    totalsMat(diagInd) = -sum(totalsMat,2);
    nonEmpty = find(totalsVec>0);
    d(nonEmpty,:) = bsxfun(@ldivide,...
        totalsVec(nonEmpty),totalsMat(nonEmpty,:));
    transMatGrpAvg(cdIdx,:,:) = d; 
        
    p = 3+ 9*(cdIdx-1);
    xlswrite(filename2, d, 'Avg',strcat('k','',num2str(p),''));
    clear d
    
    grp = strcat('',num2str(cdIdx),'');
    xlswrite(filename2, {strcat('Group', '',grp,'')}, 'Avg',strcat('A','',num2str(p-1),''));
    
end
